{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 动态地给实例绑定属性和方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s.name:  Zach\n",
      "s.age:  25\n"
     ]
    }
   ],
   "source": [
    "from types import MethodType\n",
    "\n",
    "class Student(object):\n",
    "    pass\n",
    "\n",
    "def set_age(self, age):\n",
    "    self.age = age\n",
    "    \n",
    "s = Student()\n",
    "s.name = \"Zach\" # 动态的绑定一个属性\n",
    "s.set_age = MethodType(set_age, s) # 动态的绑定一个方法\n",
    "s.set_age(25)\n",
    "print(\"s.name: \", s.name)\n",
    "print(\"s.age: \", s.age)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 动态地给class绑定属性和方法\n",
    "\n",
    "给class动态的绑定属性和方法，这样每一个继承的实例都会拥有该属性和方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "s.name:  Zach\n",
      "s.age:  25\n"
     ]
    }
   ],
   "source": [
    "class Student(object):\n",
    "    pass\n",
    "\n",
    "def set_age(self, age):\n",
    "    self.age = age\n",
    "\n",
    "def __init__(self, name):\n",
    "    self.name = name\n",
    "    \n",
    "Student.__init__ = __init__ # 动态给class绑定属性和方法\n",
    "Student.set_age = set_age\n",
    "\n",
    "s = Student(\"Zach\")\n",
    "s.set_age(25)\n",
    "print(\"s.name: \", s.name)\n",
    "print(\"s.age: \", s.age)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 使用`__slots__`：限制实例添加的属性\n",
    "\n",
    "使用`__slot__`定义的属性仅仅只对当前实例有效，对子类是无效的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'Student' object has no attribute 'score'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-2-4b43135a2f21>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      5\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'Zach'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      6\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mage\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m25\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 7\u001b[1;33m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscore\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m98\u001b[0m \u001b[1;31m# 属性score不在__slot__内, 会报错AttributeError\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      9\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"s.score: \"\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mscore\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# 哈,怎么仍然能够打印出来呢，奇怪？\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'Student' object has no attribute 'score'"
     ]
    }
   ],
   "source": [
    "class Student(object):\n",
    "    __slots__ = ('name', 'age') # 用tuple定义运行添加的属性\n",
    "    \n",
    "s = Student()\n",
    "s.name = 'Zach'\n",
    "s.age = 25\n",
    "s.score = 98 # 属性score不在__slot__内, 会报错AttributeError\n",
    "\n",
    "print(\"s.score: \", s.score) # 哈,怎么仍然能够打印出来呢，奇怪？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
